package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.mapper.*;
import com.woniu.pojo.*;
import com.woniu.service.IServiceDisplayService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务展示表 服务实现类
 * </p>
 *
 * @author xiaotang
 * @since 2022-10-18
 */
@Slf4j
@Service
public class ServiceDisplayServiceImpl extends ServiceImpl<ServiceDisplayMapper, ServiceDisplay> implements IServiceDisplayService {

    @Autowired
    private ServiceDisplayMapper serviceDisplayMapper;

    @Autowired
    private ServicesMapper servicesMapper;

    @Autowired
    private UserServiceImpl userServiceImpl;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserDetailMapper userDetailMapper;

    @Autowired
    private UserLabelMapper userLabelMapper;

    @Autowired
    private FamilyMapper familyMapper;
    /**
     * 分页搜索
     * @param currentPage
     * @param pageSize
     * @param queryServiceDisplayVo
     * @return
     */
    @Override
    public PageResult search(Integer currentPage, Integer pageSize, QueryServiceDisplayVo queryServiceDisplayVo) throws ParseException {
        QueryWrapper<ServiceDisplay> wrapper = new QueryWrapper<>();
        if(queryServiceDisplayVo != null){
            if(queryServiceDisplayVo.getServiceStatusId() != null){
                wrapper.eq("service_status_id", queryServiceDisplayVo.getServiceStatusId());
            }
            if(queryServiceDisplayVo.getOrganizationId() != null){
                wrapper.eq("organization_id", queryServiceDisplayVo.getOrganizationId());
            }
            if(queryServiceDisplayVo.getTeamId() != null){
                wrapper.eq("team_id", queryServiceDisplayVo.getTeamId());
            }
            if(queryServiceDisplayVo.getPackerId() != null){
                wrapper.eq("package_id", queryServiceDisplayVo.getPackerId());
            }
            if(queryServiceDisplayVo.getStartTime() != null){
                wrapper.gt("appointment_time", queryServiceDisplayVo.getStartTime());
            }
            if(queryServiceDisplayVo.getEndTime() != null){
                wrapper.lt("appointment_time", queryServiceDisplayVo.getEndTime());
            }
            if(!StringUtils.isEmpty(queryServiceDisplayVo.getName())){
                wrapper.like("name", queryServiceDisplayVo.getName());
            }
        }
        IPage<ServiceDisplay> iPage = new Page<>(currentPage, pageSize);
        log.debug("分页查询之前");
        IPage<ServiceDisplay> serviceDisplayIPage = serviceDisplayMapper.selectPage(iPage, wrapper);
        log.debug("serviceDisplayIPage" + serviceDisplayIPage.getRecords());
        List<ServiceDisplay> serviceDisplayIPageRecords = serviceDisplayIPage.getRecords();
        List<QueryServiceDetailVo> queryServiceDetailVoList = new ArrayList<>();
        for (ServiceDisplay serviceDisplayIPageRecord : serviceDisplayIPageRecords) {
            Long id = serviceDisplayIPageRecord.getId();
            QueryServiceDetailVo queryServiceDetailVo = queryById(id);
            queryServiceDetailVoList.add(queryServiceDetailVo);
        }
        return new PageResult(serviceDisplayIPage.getTotal(), queryServiceDetailVoList);
    }

    /**
     * 根据服务id查询服务信息
     * @param servicesId
     * @return
     */
    @Override
    public QueryServiceDetailVo queryById(Long servicesId) throws ParseException {
        QueryServiceDetailVo queryServiceDetailVo = new QueryServiceDetailVo();
        ServiceDisplay serviceDisplay = serviceDisplayMapper.selectById(servicesId);
        Long userId = serviceDisplay.getUserId();;
        UserInformationVo people = userServiceImpl.getPeopleById(userId);
        log.debug("searchById前");
        QueryServiceInfortmationVo queryServiceInfortmationVo = servicesMapper.searchById(servicesId);
        log.debug("searchById后");
        System.out.println("people: " + people);
        System.out.println("queryServiceInformationVo: " + queryServiceDetailVo);
        BeanUtils.copyProperties(people, queryServiceDetailVo);
        BeanUtils.copyProperties(queryServiceInfortmationVo, queryServiceDetailVo);
        queryServiceDetailVo.setUserId(userId);
        return queryServiceDetailVo;
    }


    /**
     * 新增服务
     * @param serviceSubmitVo
     * @return
     */
    public Boolean add(ServiceSubmitVo serviceSubmitVo) {
        log.debug("进入add: ");
        log.debug("serviceSubmitVo: " + serviceSubmitVo);
        UserInformationVo userInformationVo = new UserInformationVo();
        BeanUtils.copyProperties(serviceSubmitVo, userInformationVo);
        Long userId = serviceSubmitVo.getUserId();
        userInformationVo.setId(userId);
        log.debug("转化完成");
        System.out.println("userInformationVo的值为：" + userInformationVo);
        // 转换为user
        User user = userMapper.selectById(userId);
        String tempUserName = user.getName();
        BeanUtils.copyProperties(userInformationVo, user);
        user.setPackageId(serviceSubmitVo.getPageId());
        user.setOrganizationId(serviceSubmitVo.getTitleId());
        user.setAddress(serviceSubmitVo.getAddressNow());
        userMapper.updateById(user);
        // 转换为userDetail
        QueryWrapper<UserDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        UserDetail userDetail = userDetailMapper.selectOne(wrapper);
        BeanUtils.copyProperties(userInformationVo, userDetail);
        log.debug("转化完成");
        System.out.println("userDetail的值为：" + userDetail);
        userDetailMapper.updateById(userDetail);
        // 修改标签表
        log.debug("开始修改标签表");
        if(serviceSubmitVo.getLabelList() != null && serviceSubmitVo.getLabelList().size() > 0){
            QueryWrapper<UserLabel> userLabelWrapper = new QueryWrapper<>();
            userLabelWrapper.eq("user_id", userId);
            userLabelMapper.delete(userLabelWrapper);
            List<Label> labelList = serviceSubmitVo.getLabelList();
            for (Label label : labelList) {
                UserLabel userLabel = new UserLabel();
                userLabel.setUserId(userId);
                userLabel.setLabelId(label.getId());
                userLabelMapper.insert(userLabel);
            }
        }

        // 修改家庭成员关系表
        if(serviceSubmitVo.getFamilyList() != null && serviceSubmitVo.getFamilyList().size() > 0){
            log.debug("开始修改家庭表");
            QueryWrapper<Family> familyWrapper = new QueryWrapper<>();
            familyWrapper.eq("user_id", userId);
            familyMapper.delete(familyWrapper);
            List<Family> familyList = serviceSubmitVo.getFamilyList();
            for (Family family : familyList) {
                familyMapper.insert(family);
            }
        }
        serviceSubmitVo.setSubmitTime(new Date());
        Services services = new Services();
        BeanUtils.copyProperties(serviceSubmitVo, services);
        services.setServiceStatusId(serviceSubmitVo.getServiceStatusId());
        servicesMapper.insert(services);
        Long id = services.getId();
        log.debug("id的值为：" + id);

        ServiceDisplay serviceDisplay = new ServiceDisplay();
        BeanUtils.copyProperties(serviceSubmitVo, serviceDisplay);
        serviceDisplay.setName(user.getName());
        serviceDisplay.setOrganizationId(serviceSubmitVo.getTitleId());
        serviceDisplay.setPackageId(serviceSubmitVo.getPageId());
        serviceDisplay.setTeamId(serviceSubmitVo.getTitleId());
        serviceDisplay.setId(id);
        serviceDisplay.setName(tempUserName);
        serviceDisplayMapper.insert(serviceDisplay);
        return true;
    }

    /**
     * 修改服务
     * @param serviceSubmitVo
     * @return
     */
    @Override
    public Boolean modify(ServiceSubmitVo serviceSubmitVo) {
        log.debug("serviceSubmitVo: " + serviceSubmitVo);
        UserInformationVo userInformationVo = new UserInformationVo();
        BeanUtils.copyProperties(serviceSubmitVo, userInformationVo);
        Long userId = serviceSubmitVo.getUserId();
        userInformationVo.setId(userId);
        // 转换为user
        User user = userMapper.selectById(userId);
        log.debug("user: " + user);
        String tempUserName = user.getName();
        BeanUtils.copyProperties(userInformationVo, user);
        user.setPackageId(serviceSubmitVo.getPageId());
        user.setPackageId(serviceSubmitVo.getPageId());
        user.setOrganizationId(serviceSubmitVo.getTitleId());
        user.setAddress(serviceSubmitVo.getAddressNow());
        userMapper.updateById(user);
        // 转换为userDetail
        QueryWrapper<UserDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        UserDetail userDetail = userDetailMapper.selectOne(wrapper);
        BeanUtils.copyProperties(userInformationVo, userDetail);
        log.debug("转化完成");
        System.out.println("userDetail的值为：" + userDetail);
        userDetailMapper.updateById(userDetail);
        // 修改标签表
        if(serviceSubmitVo.getLabelList() != null && serviceSubmitVo.getLabelList().size() > 0){
            log.debug("开始修改标签表");
            QueryWrapper<UserLabel> userLabelWrapper = new QueryWrapper<>();
            userLabelWrapper.eq("user_id", userId);
            userLabelMapper.delete(userLabelWrapper);
            List<Label> labelList = serviceSubmitVo.getLabelList();
            for (Label label : labelList) {
                UserLabel userLabel = new UserLabel();
                userLabel.setUserId(userId);
                userLabel.setLabelId(label.getId());
                userLabelMapper.insert(userLabel);
            }
        }

        if(serviceSubmitVo.getFamilyList() != null && serviceSubmitVo.getFamilyList().size() > 0){
            // 修改家庭成员关系表
            QueryWrapper<Family> familyWrapper = new QueryWrapper<>();
            familyWrapper.eq("user_id", userId);
            familyMapper.delete(familyWrapper);
            List<Family> familyList = serviceSubmitVo.getFamilyList();
            for (Family family : familyList) {
                familyMapper.insert(family);
            }
        }


        Services services = new Services();
        BeanUtils.copyProperties(serviceSubmitVo, services);
        servicesMapper.updateById(services);
        log.debug("id的值为：" + services.getId());

        ServiceDisplay serviceDisplay = new ServiceDisplay();
        BeanUtils.copyProperties(serviceSubmitVo, serviceDisplay);

        serviceDisplay.setName(tempUserName);
        serviceDisplay.setOrganizationId(serviceSubmitVo.getTitleId());
        serviceDisplay.setPackageId(serviceSubmitVo.getPageId());
        serviceDisplay.setTeamId(serviceSubmitVo.getTitleId());

        serviceDisplayMapper.updateById(serviceDisplay);

        return true;
    }
}
